#!/bin/bash

SHDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "脚本路径为: $SHDIR"

sudo chown $(whoami):$(whoami) -R $SHDIR
sudo  mkdir -p /data/mysql/data
sudo  mkdir -p /data/mongo/data /data/mongo/log
sudo chown $(whoami):$(whoami) -R /data
chmod 777 -R /data

echo "检查Docker......"
docker -v
if [ $? -eq  0 ]; then
    echo "检查到Docker已安装!"
else
    echo "安装docker环境..."
    curl -sSL https://get.daocloud.io/docker | sh
    echo "安装docker环境...安装完成!"
fi

# 检查swarm是否运行
exist=`docker node ls |grep Ready`
if [ "${exist}" == "" ]; then
    echo " 初始化docker swarm容器编排服务 ... "
    sudo docker swarm init 
    sleep 3
fi
echo " [docker swarm] 已启动运行 ..."

docker network create --subnet 100.0.0.0/24 --scope swarm blog-subnet || true
cd $SHDIR
echo -e "\n-----\nstop running basic service ... "
docker stack remove basic
sleep 1
echo -e "\n-----\nstart running basic service ... "
docker stack deploy -c docker-compose-basic.yml basic
cd -